package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Ability;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.AbilityService;
import com.zzty.txj1.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/ability")
public class AbilityController {
    private static final Logger logger = LoggerFactory.getLogger(AbilityController.class);

    @Autowired
    private AbilityService abilityService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/addAbilityByUserId")
    public String initAbility(User user) {
        logger.info("ability--controller ============ addAbilityByUserId OpenId :" + user.getOpenId());
        User user1 = userService.getUserByOpenId(user.getOpenId());

        Ability ability = abilityService.getAbilityByUserId(user1);
        String result = "success";
        if(ability!=null){
            Integer count = abilityService.addAbility(user1);
            if (count == 1) {
                result = "success";
            }
        }

        logger.info("ability--controller ============ addAbilityByUserId result :" + result);
        return result;
    }

    @RequestMapping(value = "/getAbilityByUserId")
    public Ability getAbilityByUserId(User user) {
        logger.info("ability--controller ============ getAbilityByUserId user :" + user.toString());

        Ability ability = abilityService.getAbilityByUserId(user);
        logger.info("ability--controller ============ getAbilityByUserId ability :" + ability.toString());
        return ability;
    }
}
